package com.eat.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.eat.dto.SetmealDTO;
import com.eat.dto.SetmealPageQueryDTO;
import com.eat.entity.Category;
import com.eat.entity.Setmeal;
import com.eat.entity.SetmealDish;
import com.eat.exception.SetmealEnableFailedException;
import com.eat.mapper.CategoryMapper;
import com.eat.mapper.SetmealDishMapper;
import com.eat.mapper.SetmealMapper;
import com.eat.result.PageResult;
import com.eat.service.SetmealService;
import com.eat.vo.DishItemVO;
import com.eat.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImp implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public List<Setmeal> list(Setmeal setmeal) {

       List<Setmeal> setmeals = setmealMapper.getByCategoryId(setmeal);

        return setmeals;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemById(Long id) {

        List<DishItemVO> dishById = setmealDishMapper.getSetmealDishById(id);

        return dishById;
    }

    /**
     * 添加套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveWithSetmealDish(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.saveSetmeal(setmeal);

        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();

        if (setmealDishes==null){
            throw new SetmealEnableFailedException("套餐不能为空");
        }
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.saveSetmealDish(setmealDish);
        }


    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<SetmealVO> page= setmealMapper.page(setmealPageQueryDTO);

        List<SetmealVO> result = page.getResult();
        for (SetmealVO setmealVO : result) {
            Category category =  categoryMapper.getSetmealCateById(setmealVO.getCategoryId());
            setmealVO.setCategoryName(category.getName());
        }
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐起售停售
     * @param status
     * @param id
     */
    @Override
    public void putStatus(Integer status, Long id) {

        setmealMapper.putStatus(status,id);

    }

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void putSetmeal(SetmealDTO setmealDTO) {

        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.putSetmeal(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //判断
        if (setmealDishes!=null && setmealDishes.size()>0){
            Long id = setmealDTO.getId();
            setmealDishMapper.deleteBySetmealId(id);
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDishMapper.saveSetmealDish(setmealDish);
            }
        }

    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDTO getSetmealById(Long id) {

        SetmealDTO setmealDTO = setmealMapper.getById(id);

        List<SetmealDish> setmealDishes = setmealDishMapper.getDishById(id);

        setmealDTO.setSetmealDishes(setmealDishes);

        return setmealDTO;
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Override
    public void deleteByIds(List<Long> ids) {

        setmealMapper.deleteByIds(ids);

        setmealDishMapper.deleteBySetmealIds(ids);

    }
}
